package com.wx.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wx.pojo.Cart;
import com.wx.service.CartService;
/**
 * 购物车后端
 * @author gzcadmin
 *
 */
@Controller
@RequestMapping("/cart")
public class CartController {

	@Autowired
	private CartService cartService;
	
	@RequestMapping(value="/addCart",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> addCart(
			@RequestParam("userId")String userId,
			@RequestParam("goodsId")Integer sourceId,
			@RequestParam("name")String sourceName,
			@RequestParam("number")Integer sourceCount,
			@RequestParam("pic")String sourceImage,
			@RequestParam("price")Double sourcePrice){
		//判断用户是否登录
		Map<String,Object> modelMap=new HashMap<String,Object>();
		if(userId==null || userId=="") {
			modelMap.put("errno", 1);
			modelMap.put("errMsg", "该用户没有登录");
		}
		Cart cart=new Cart();
		cart.setUserId(userId);
		cart.setSourceId(sourceId);
		cart.setSourceName(sourceName);
		
		cart.setSourceCount(sourceCount);
		cart.setSourceImage(sourceImage);
		cart.setSourcePrice(sourcePrice);
		//判断商品单价和数量是否传值过来，若存在计算单个商品的总价
		if(sourcePrice!=0 && sourcePrice!=null) {
			if(sourceCount!=0 && sourceCount!=null) {
				cart.setSourceFee(sourcePrice*sourceCount);
			}
		}
		
		System.out.println(cart);
		
		String msg=cartService.addCart(cart);
		//通过返回的字符串判断是否插入成功
		if(msg.equals("insert")) {
			if(cart.getCartId()!=null) {
				modelMap.put("errno", 0);//成功返回前端设定字符编号
			}
		}else if(msg.equals("update")) {
			modelMap.put("errno", 0);
		}else if(msg.equals("failUpdate")) {
			modelMap.put("errno", 2);//代表加入购物车原有商品信息失败
		}else {
			modelMap.put("errno", 1);//加入购物车失败
		}
		return modelMap;
	}
	
	/**
	 * 通过用户id查询购物车商品
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/showCart",method=RequestMethod.GET)
	@ResponseBody
	public Map<String,Object>findCartByUserId(@RequestParam("userId") String userId){
		Map<String,Object>modelMap = new HashMap<String,Object>();
		if(userId=="" || userId==null) {
			modelMap.put("errno", 1);//用户还没有登录
			return modelMap;
		}
		System.out.println("userId: "+userId);
		List<Cart> cartList=cartService.findCartByUserId(userId);
		modelMap.put("errno", 0);
		modelMap.put("cartList",cartList);
		return modelMap;
	}
	/**
	 * 删除购物车商品通过购物车id
	 * @param cartId
	 * @return
	 */
	@RequestMapping(value="/removeSourceOne",method=RequestMethod.GET)
	@ResponseBody
	public Map<String,Object>removeSourceOne(@RequestParam("cartId")Integer cartId){
		Map<String,Object>modelMap = new HashMap<String,Object>();
		cartService.removeSourceBycartId(cartId);
		System.out.println("cartId: "+cartId);
		modelMap.put("errno", 0);
		return modelMap;
	}
	/**
	 * 通过用户id移除该用户全部的购物车商品
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/removeSourceAll",method=RequestMethod.GET)
	@ResponseBody
	public Map<String,Object>removeSourceAll(@RequestParam("userId")Integer userId){
		Map<String,Object>modelMap = new HashMap<String,Object>();
		cartService.removeSourceAllByUserId(userId);
		modelMap.put("errno", 0);
		return modelMap;
	}
	
}
